package com.example.jin.myapplication.NerdLauncher;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.jin.myapplication.R;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by jin on 2016/6/19.
 */
public class NerdLauncherFragment extends Fragment {
    private List<ResolveInfo> mActivities;
    private PackageManager mManager;
    public static Fragment newInstance(){
        return new NerdLauncherFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mManager=getActivity().getPackageManager();
        Intent intent=new Intent();
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setAction(Intent.ACTION_MAIN);
        mActivities=mManager.queryIntentActivities(intent,0);
        Collections.sort(mActivities, new Comparator<ResolveInfo>() {
            @Override
            public int compare(ResolveInfo lhs, ResolveInfo rhs) {
                return String.CASE_INSENSITIVE_ORDER.compare(lhs.loadIcon(mManager).toString(),rhs.loadIcon(mManager).toString());
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=null;
        view=inflater.inflate(R.layout.fragment_nerdlauncher,container,false);
        RecyclerView recyclerView=(RecyclerView)view.findViewById(R.id.nerdlaucher_recycleview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new NerdLauncherAdapter());
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    private class NerdLauncherViewHolder extends RecyclerView.ViewHolder{
        private View mItemView;
        public NerdLauncherViewHolder(View itemView) {
            super(itemView);
            mItemView=itemView;
        }
        public void bind(final ResolveInfo info){
            if(mItemView!=null) {
                TextView view = (TextView) mItemView.findViewById(android.R.id.text1);
                view.setText(info.loadLabel(mManager));
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent();
                        intent.setAction(Intent.ACTION_MAIN);
                        intent.setClassName(info.activityInfo.applicationInfo.packageName,info.activityInfo.name);
                        startActivity(intent);
                    }
                });
            }
        }
    }
    private class NerdLauncherAdapter extends RecyclerView.Adapter<NerdLauncherViewHolder>{

        @Override
        public NerdLauncherViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=null;
            view=LayoutInflater.from(getActivity()).inflate(android.R.layout.simple_list_item_1,parent,false);
            NerdLauncherViewHolder holder=new NerdLauncherViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(NerdLauncherViewHolder holder, int position) {
            holder.bind(mActivities.get(position));
        }

        @Override
        public int getItemCount() {
            return mActivities.size();
        }
    }
}
